package com.anolesoft.yr.mbus.prod.model.crtl;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.prod.model.dto.ProdServiceQueryDTO;
import com.anolesoft.yr.mbus.prod.model.service.ProdServiceService;
import com.anolesoft.yr.mbus.prod.model.vo.ProdServiceQueryVO;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@CrossOrigin
@RequestMapping(value = "/prodService")
public class ProdServiceController {

    @Autowired
    ProdServiceService prodServiceService;

    //根据主键查询产品服务项名称
    @RequestMapping(value = "/queryServiceName", method = RequestMethod.POST)
    @ResponseBody
    public String queryProdServiceName(String objuid) throws Exception {
        List<ProdServiceQueryVO> prodServiceQueryVOList = prodServiceService.queryProdServiceName(objuid);

        String jsons = "";
        jsons += "[";
        if (prodServiceQueryVOList.size() > 0) {
            for (int i = 0; i < prodServiceQueryVOList.size(); i++) {
                ProdServiceQueryVO prodServiceQueryVO = (ProdServiceQueryVO) prodServiceQueryVOList.get(i);
                jsons += new Gson().toJson(prodServiceQueryVO);
                if (i < (prodServiceQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";

        return jsons;
    }

    //根据主键查询产品服务项信息
    @RequestMapping(value = "/queryServiceByUid", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryServiceByUid(@RequestBody ProdServiceQueryDTO prodServiceQueryDTO) throws Exception {
        Map<String, Object> maps = prodServiceService.queryProdServiceByUid(prodServiceQueryDTO.getObjuid());
        ProdServiceQueryVO prodServiceQueryVO = (ProdServiceQueryVO) maps.get("prodServiceQueryVO");

        String jsons = "";
        if (prodServiceQueryVO != null) {
            jsons += "[";
            jsons += new Gson().toJson(prodServiceQueryVO);
            jsons += "]";
        }

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //查询产品服务项名称
    @RequestMapping(value = "/queryProdServiceName2", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdServiceName2(String prodName) throws Exception {
        List ProdServiceQueryVOList = prodServiceService.queryProdServiceName2(prodName);

        String jsons = "";
        jsons += "[";
        if (ProdServiceQueryVOList.size() > 0) {
            for (int i = 0; i < ProdServiceQueryVOList.size(); i++) {
                ProdServiceQueryVO prodServiceQueryVO = (ProdServiceQueryVO) ProdServiceQueryVOList.get(i);
                jsons += new Gson().toJson(prodServiceQueryVO);
                if (i < (ProdServiceQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

}
